package cl.golive.d2menu.pantallas;

import cl.golive.d2menu.obj.Compra;
import cl.golive.d2menu.utils.modeloUtils.CompraTableModel;
import cl.golive.d2menu.utils.tableUtils.AccionesTablaUtil;
import cl.golive.d2menu.utils.tableUtils.UtilesTabla;
import javax.swing.JOptionPane;

/**
 *
 * @author whyem
 */
public class PantallaResumenCompra_ extends javax.swing.JDialog {

    public  CompraTableModel modelo;

    public PantallaResumenCompra_(java.awt.Frame parent, boolean modal, CompraTableModel _modelo) {
        super(parent, modal);
        initComponents();
        initComponentesPersonalizados();
        this.modelo = _modelo;
        cargarResumenDeCompra(modelo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pnlBotones = new javax.swing.JPanel();
        botoneraAcciones = new javax.swing.JToolBar();
        botonRealizarPedido = new javax.swing.JButton();
        botonQuitarDeCompra = new javax.swing.JButton();
        botonVolver = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        pnlTitulo = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        labelTotal = new javax.swing.JLabel();
        labelSumaTotal = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        pnlResumenCompra = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grillaResumenCompra = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 600));
        setUndecorated(true);

        jPanel1.setLayout(new java.awt.BorderLayout());

        pnlBotones.setBackground(new java.awt.Color(0, 0, 0));
        pnlBotones.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlBotones.setLayout(new java.awt.BorderLayout());

        botoneraAcciones.setBackground(new java.awt.Color(0, 0, 0));
        botoneraAcciones.setFloatable(false);
        botoneraAcciones.setOrientation(1);
        botoneraAcciones.setRollover(true);

        botonRealizarPedido.setBackground(new java.awt.Color(0, 0, 0));
        botonRealizarPedido.setForeground(new java.awt.Color(255, 255, 255));
        botonRealizarPedido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenesApp/B_imprimir.png"))); // NOI18N
        botonRealizarPedido.setText("Realizar pedido");
        botonRealizarPedido.setFocusable(false);
        botonRealizarPedido.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonRealizarPedido.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botonRealizarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRealizarPedidoActionPerformed(evt);
            }
        });
        botoneraAcciones.add(botonRealizarPedido);

        botonQuitarDeCompra.setBackground(new java.awt.Color(0, 0, 0));
        botonQuitarDeCompra.setForeground(new java.awt.Color(255, 255, 255));
        botonQuitarDeCompra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenesApp/B_eliminar.png"))); // NOI18N
        botonQuitarDeCompra.setText("Quitar del pedido");
        botonQuitarDeCompra.setFocusable(false);
        botonQuitarDeCompra.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonQuitarDeCompra.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botonQuitarDeCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonQuitarDeCompraActionPerformed(evt);
            }
        });
        botoneraAcciones.add(botonQuitarDeCompra);

        botonVolver.setBackground(new java.awt.Color(0, 0, 0));
        botonVolver.setForeground(new java.awt.Color(255, 255, 255));
        botonVolver.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imagenesApp/B_volver.png"))); // NOI18N
        botonVolver.setText("Volver");
        botonVolver.setFocusable(false);
        botonVolver.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonVolver.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botonVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonVolverActionPerformed(evt);
            }
        });
        botoneraAcciones.add(botonVolver);

        pnlBotones.add(botoneraAcciones, java.awt.BorderLayout.CENTER);

        jPanel1.add(pnlBotones, java.awt.BorderLayout.LINE_END);

        jPanel3.setLayout(new java.awt.BorderLayout());

        pnlTitulo.setBackground(new java.awt.Color(0, 0, 0));
        pnlTitulo.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlTitulo.setForeground(new java.awt.Color(255, 255, 255));
        pnlTitulo.setLayout(new java.awt.BorderLayout());

        jLabel2.setFont(new java.awt.Font("Corbel", 1, 24));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Resumen de la Compra");
        pnlTitulo.add(jLabel2, java.awt.BorderLayout.PAGE_START);

        jPanel3.add(pnlTitulo, java.awt.BorderLayout.PAGE_START);

        jPanel5.setBackground(new java.awt.Color(0, 0, 0));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel5.setPreferredSize(new java.awt.Dimension(450, 80));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel6.setBackground(new java.awt.Color(0, 0, 0));
        jPanel6.setLayout(new java.awt.GridLayout(1, 0));

        jLabel3.setText("jLabel3");
        jPanel6.add(jLabel3);

        labelTotal.setBackground(new java.awt.Color(0, 0, 0));
        labelTotal.setFont(new java.awt.Font("Arial Narrow", 1, 16));
        labelTotal.setForeground(new java.awt.Color(255, 255, 255));
        labelTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelTotal.setText("TOTAL:");
        jPanel6.add(labelTotal);

        labelSumaTotal.setFont(new java.awt.Font("Arial Narrow", 1, 16));
        labelSumaTotal.setForeground(new java.awt.Color(255, 255, 255));
        labelSumaTotal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelSumaTotal.setText("$");
        jPanel6.add(labelSumaTotal);

        jPanel5.add(jPanel6, java.awt.BorderLayout.CENTER);

        jPanel11.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 67, Short.MAX_VALUE)
        );

        jPanel5.add(jPanel11, java.awt.BorderLayout.LINE_END);

        jPanel12.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 67, Short.MAX_VALUE)
        );

        jPanel5.add(jPanel12, java.awt.BorderLayout.LINE_START);

        jPanel3.add(jPanel5, java.awt.BorderLayout.PAGE_END);

        pnlResumenCompra.setBackground(new java.awt.Color(0, 0, 0));
        pnlResumenCompra.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlResumenCompra.setLayout(new java.awt.BorderLayout());

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 541, Short.MAX_VALUE)
        );

        pnlResumenCompra.add(jPanel2, java.awt.BorderLayout.LINE_START);

        jPanel4.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 541, Short.MAX_VALUE)
        );

        pnlResumenCompra.add(jPanel4, java.awt.BorderLayout.LINE_END);

        jScrollPane1.setBackground(new java.awt.Color(0, 0, 0));
        jScrollPane1.setForeground(new java.awt.Color(255, 255, 255));

        grillaResumenCompra.setBackground(new java.awt.Color(0, 0, 0));
        grillaResumenCompra.setFont(new java.awt.Font("Arial Narrow", 1, 16));
        grillaResumenCompra.setForeground(new java.awt.Color(255, 255, 255));
        grillaResumenCompra.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}
            },
            new String [] {
                "Platos", "Precio"
            }
        ));
        grillaResumenCompra.setRowHeight(30);
        grillaResumenCompra.setSelectionBackground(new java.awt.Color(255, 255, 204));
        grillaResumenCompra.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPane1.setViewportView(grillaResumenCompra);

        pnlResumenCompra.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel3.add(pnlResumenCompra, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonVolverActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_botonVolverActionPerformed

    private void botonQuitarDeCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonQuitarDeCompraActionPerformed
        Compra platoCompra = AccionesTablaUtil.obtenerPlatoSeleccionadoCompra(grillaResumenCompra);
        if(platoCompra != null) {
           AccionesTablaUtil.quitarPlatoDeListaPedido(grillaResumenCompra,platoCompra);
           calcularTotalCompra();
        }
    }//GEN-LAST:event_botonQuitarDeCompraActionPerformed

    private void botonRealizarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRealizarPedidoActionPerformed
        JOptionPane.showMessageDialog(this,"No se ha encontrado una impresora conectada","Advertencia",JOptionPane.WARNING_MESSAGE);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_botonRealizarPedidoActionPerformed

    private void cargarResumenDeCompra(CompraTableModel modelo) {
        grillaResumenCompra.setModel(modelo);
        UtilesTabla.setOcultarColumnasJTable(grillaResumenCompra, new int[]{1,2,4,6});
        calcularTotalCompra();
    }

     private void initComponentesPersonalizados() {
         this.setSize(960,600);
         this.setLocationRelativeTo(null);
    }

     private void calcularTotalCompra() {
         int sumaTotal=0;
         for(int i=0;i<modelo.getRowCount();i++) {
            Compra compra = modelo.getItemPlatoCompraAt(i, modelo);
            sumaTotal += compra.getPrecio();
         }
         labelSumaTotal.setText("$ "+sumaTotal);
         bloquearBotonesSinPlatos(sumaTotal);
     }

     private void bloquearBotonesSinPlatos(int suma) {
        if(suma<=0) {
            botonRealizarPedido.setEnabled(false);
            botonQuitarDeCompra.setEnabled(false);
        }else {
            botonRealizarPedido.setEnabled(true);
            botonQuitarDeCompra.setEnabled(true);
        }
     }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PantallaResumenCompra_ dialog = new PantallaResumenCompra_(new javax.swing.JFrame(), true, new CompraTableModel());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonQuitarDeCompra;
    private javax.swing.JButton botonRealizarPedido;
    private javax.swing.JButton botonVolver;
    private javax.swing.JToolBar botoneraAcciones;
    private javax.swing.JTable grillaResumenCompra;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelSumaTotal;
    private javax.swing.JLabel labelTotal;
    private javax.swing.JPanel pnlBotones;
    private javax.swing.JPanel pnlResumenCompra;
    private javax.swing.JPanel pnlTitulo;
    // End of variables declaration//GEN-END:variables

}
